<template>
  <div class="row">
    <div v-for="column in columnList" :key="column._id" class="col-4">
      <div class="card h-100 shadow-sm">
        <div class="card-body text-center">
          <img :src="column.avatar" :alt="这是个图片">
          <h5 class="card-title">{{ column.title }}</h5>
          <p class="card-text text-left">{{ column.description }}</p>
          <router-link :to="`/column/${column._id}`" class="btn btn-outline-primary">进入专栏</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, computed } from "vue";
import { require } from "@/utils/require";

export interface ColumnProps {
  id: number;
  title: string;
  avatar?: string;
  description: string;
}

export default {
  name: "ColumnList",
  // import 引入的组件需要注入到对象中才能使用
  components: {},
  props: {
    list: {
      type: Array as PropType<ColumnProps[]>,
      require: true
    }
  },
  // 计算属性 类似于 data 概念
  computed: {},
  // 监控 data 中的数据变化
  watch: {},
  // 方法集合
  methods: {},
  // setup在beforeCreate之前执行一次，this是undefined。
  setup(props) {

    const columnList = computed(() => {
      return props.list.map(column => {
        // addColumnAvatar(column, 50, 50)
        if (!column.avatar) {
          column.avatar = require("@/assets/column.jpg");
        }
        return column;
      });
    });
    const bgImgSrc = require("@/assets/column.jpg");
    return {
      columnList,
      bgImgSrc
    };
  }
};
</script>

<style scoped>
</style>
